﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Data;

public partial class KMrpt1 : BaseRptPage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new System.EventHandler(Page_Load);
    } 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //bindshop("");
            this.sdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
        string aciton = Request.Params["aciton"];
        switch (aciton)
        {
            case "pos_sale_chart":
                GetChartsData();
                break;
        }
    }

    private void GetChartsData()
    {
        string val = " ";
        if (this.business.Text.Trim() != "")
        {
            val += "(";
            string[] list = business.Text.Split(';');
            foreach (string s in list)
            {
                val += "''" + s + "'',";
            }
            val = val.TrimEnd(',');
            val += ")";
        }
        DataSet ds = db.DM_GetDateSet("p_DailyPayAmt '2015-10-13','" + val + "'");
        /*
         * {"kcmc":{"categories":["北京路店","武汉店","长春店","佛山店"]},
            "score":[{"data":[75,84,90,96,86,100],"name":"本期销售额"},{"data":[83,87,88,91,83,100],"name":"目标销售额"}]}
         * */
        string sbr = "{\"shopname\":{\"categories\":[\"北京路店\",\"武汉店\",\"长春店\",\"佛山店\"]},"+
            "\"payamt\":[{\"data\":[75,84,90,96],\"name\":\"本期销售额\"},{\"data\":[83,87,88,91],\"name\":\"目标销售额\"}]}";
        string shopStr = "";
        string moneyStr = "";
        string mbStr = "";
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            shopStr += "\""+dr["shopname"]+"\",";
            moneyStr += dr["allpayamt"].ToString()+",";
            mbStr += dr["targetmoney"].ToString()+",";
        }
        //sbr = "{\"shopname\":{\"categories\":["+shopStr.TrimEnd(',')+"]}," +
        //"\"payamt\":[{\"data\":["+moneyStr.TrimEnd(',')+"],\"name\":\"本期销售额\"},{\"data\":["+mbStr.TrimEnd(',')+"],\"name\":\"目标销售额\"}]}";
        Response.Write(sbr);//返回礼品列表
        Response.End();
        return;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GvBind();
    }
    protected void bindshop(string shopname)
    {
        this.chkblist.Items.Clear();
        string str = "";
        if (shopname != "")
        {
            str += " and shopname like '%" + shopname + "%'";
        }
        DataSet ds = db.DM_GetDateSet("select * from Bas_Shop where 1=1 "+str+" order by shopname");
        if (ds == null)
        {
            return;
        }
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            ListItem item = new ListItem();
            item.Text = dr["shopname"].ToString();
            item.Value = dr["shopid"].ToString();
            this.chkblist.Items.Add(item);
        }
    }
    private string GetSortedFields()
    {
        string sortedField = "";
        //获取排序字段的信息
        if (ViewState["SortedField"] != null)
        {
            //如果存在排序信息，则将排序信息转换为字典存储
            Dictionary<string, string> sorted = (Dictionary<string, string>)ViewState["SortedField"];
            //便利取值
            foreach (KeyValuePair<string, string> kvp in sorted)
            {
                //key排序字段，value排序信息
                sortedField = kvp.Key + " " + kvp.Value;
            }
        }
        return sortedField;
    }

    protected void GvBind()
    {
        string sortedField = GetSortedFields();
        string val = " ";
        if (this.business.Text.Trim() != "")
        {
            val += "(";
            string[] list = business.Text.Split(';');
            foreach (string s in list)
            {
                val += "''" + s + "'',";
            }
            val = val.TrimEnd(',');
            val += ")";
        }
        if (string.IsNullOrEmpty(sortedField))
        {
            sortedField = "orderid";
        }
        this.GridView1.DataSource = db.DM_GetDateSet("KM_DailyPayAmt_rpt '" + this.sdate.Text.Trim() + "','" + val + "','"+sortedField+"'");
        this.GridView1.DataBind();
        GridviewColumsVisible(this.GridView1);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GvBind();
        GridViewToExcel(this.GridView1, "application/ms-excel", this.Title+".xls");
        GridView1.AllowPaging = true;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        GvBind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        bindshop(this.txtshopname.Text);
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        //GridViewGroup.GroupRow(GridView1, 0, 0, 3);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //判断是否进行排序
            if (ViewState["SortedField"] != null)
            {
                //恢复排序信息
                Dictionary<string, string> order = (Dictionary<string, string>)ViewState["SortedField"];
                //循环表头的单元格，判断是否有需要排序的单元格
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.Cells[i].Controls.Count > 0)
                    {
                        //是否是排序字段信息
                        LinkButton lb = e.Row.Cells[i].Controls[0] as LinkButton;
                        if (lb != null)
                        {
                            //反序和降序
                            if (order.ContainsKey(lb.CommandArgument))
                            {
                                Literal li = new Literal();
                                if (order[lb.CommandArgument] == "ASC")
                                {
                                    li.Text = "▲";
                                }
                                else
                                {
                                    li.Text = "▼";
                                }
                                //
                                e.Row.Cells[i].Controls.Add(li);
                            }
                        }
                    }
                }
            }
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text == "0")
            {
                e.Row.Cells[0].Text = "";
            }
            if (e.Row.Cells[2].Text == "总计：")
            {
                e.Row.Attributes.Add("style", "background-color: #F2F2F2; font-weight: bold;font-variant: normal; color: #000000;");
            }
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //
        Dictionary<string, string> sorted = new Dictionary<string, string>();
        //从ViewState读取上一次的排序信息。
        if (ViewState["SortedField"] == null)
        {
            //如果没有排序信息，则将当前点击的对象（即点击的排序字段）和排序信息添加到字典中
            sorted.Add(e.SortExpression, "ASC");
            //将排序信息放在字典中保存
            ViewState["SortedField"] = sorted;
        }
        else
        {
            //判断是否点击相同的排序，如果是相同的排序，
            //就是用反序
            //如果不是想通的排序，那就新的派讯，是用升序
            sorted = (Dictionary<string, string>)ViewState["SortedField"];
            if (sorted.ContainsKey(e.SortExpression))
            {
                if (sorted[e.SortExpression] == "ASC")
                {
                    sorted[e.SortExpression] = "DESC";
                }
                else
                {
                    sorted[e.SortExpression] = "ASC";
                }
            }
            else
            {
                sorted.Clear();
                sorted.Add(e.SortExpression, "ASC");
                ViewState["SortedField"] = sorted;
            }
        }

        //重新加载数据
        GvBind();

    }
}